package cn.whpu.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 字符串转日期
 */
public class StringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        DateFormat dateFormat=null;

        if (StringUtils.isEmpty(s)){
            throw new RuntimeException("请输入日期");
        }

        dateFormat =new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date =dateFormat.parse(s);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException("日期错误");
        }
    }
}
